package ware.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.feign.utils.R;
import com.feign.valid.AddGroup;
import com.feign.valid.UpdateGroup;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import ware.domain.Purchase;
import ware.service.PurchaseService;
import ware.vo.FinishVo;
import ware.vo.MergeVo;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 采购信息(Purchase)表控制层
 *
 * @author panghu
 * @since 2023-03-04 21:11:28
 */
@RestController
@RefreshScope
@RequestMapping("ware/purchase")
public class PurchaseController {

    @Autowired
    private PurchaseService purchaseService;

    @GetMapping("list")
    public R list(@RequestParam Map<String, Object> params) {
        Page<Purchase> purchasePage = purchaseService.querPage(params);
        return R.ok().put("data", purchasePage);
    }

    /**
     * 删除采购
     *
     * @param ids
     * @return
     */
    @PostMapping("remove")
    public R removeCategory(@RequestBody Long[] ids) {
        boolean result = purchaseService.removeBatchByIds(List.of(ids));
        return result ? R.ok("删除采购成功") : R.error("删除采购失败");
    }

    /**
     * 获取单个采购的数据
     *
     * @param id
     * @return
     */
    @GetMapping("info/{id}")
    public R info(@PathVariable Long id) {
        Purchase purchase = purchaseService.getById(id);
        return R.ok().put("data", purchase);
    }

    /**
     * 增加采购
     *
     * @param purchase
     * @return
     */
    @PostMapping("save")
    public R save(@Validated({AddGroup.class}) @RequestBody Purchase purchase) {
        purchase.setUpdateTime(new Date());
        purchase.setCreateTime(new Date());
        boolean result = purchaseService.save(purchase);
        return result ? R.ok("增加采购成功") : R.error("增加采购失败");
    }

    /**
     * 修改采购
     *
     * @param purchase
     * @return
     */
    @PutMapping("update")
    public R edit(@Validated({UpdateGroup.class}) @RequestBody Purchase purchase) {
        boolean result = purchaseService.updateById(purchase);
        return result ? R.ok("修改采购项成功") : R.error("修改采购项失败");
    }

    /**
     * 合并采购单
     *
     * @return
     */
    @PostMapping("merge")
    public R merge(@RequestBody MergeVo mergeVo) {
        purchaseService.mergePurchase(mergeVo);
        return R.ok("merge成功");
    }

    /**
     * 领取采购单
     *
     * @return
     */
    @PostMapping("received")
    public R received(@RequestBody List<Long> ids) {
        purchaseService.received(ids);
        return R.ok("领取成功");
    }

    @PostMapping("done")
    public R finish(@RequestBody FinishVo finishVos) {
        return purchaseService.purchaseFinish(finishVos);
    }

}

